package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.dao.HealtherNewsDao;
import com.khyr.dao.NoticeDao;
import com.khyr.dao.UserDao;
import com.khyr.entity.News;
import com.khyr.entity.Notice;
import com.khyr.entity.User;
import com.khyr.service.NoticeService;
import com.khyr.vo.NoticeVo;
import com.khyr.vo.R;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @InterfaceName: NoticeServiceImpl
 * @author: Zyl
 * @date: 2025/3/11 18:36
 * @Version: 1.0
 * @description:
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeDao noticeDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserDao userDao;
    @Autowired
    private HealtherNewsDao healtherNewsDao;

    @Override
    //公共分页查询
    public R<Page<Notice>> findNotice(NoticeVo noticeVo) {
        QueryWrapper<Notice> queryWrapper=new QueryWrapper<>();
        // 根据 noticeVo 中的条件动态构建查询条件
        if (noticeVo.getNoticeTitle() != null) {
            queryWrapper.like("notice_title", noticeVo.getNoticeTitle());
        }
        if (StringUtils.hasText(noticeVo.getStatus())) {
            queryWrapper.eq("status", noticeVo.getStatus());
        }
        if(StringUtils.hasText(noticeVo.getNoticeType())){
            queryWrapper.eq("notice_type", noticeVo.getNoticeType());
        }
        if(StringUtils.hasText(noticeVo.getCreateBy())){
            queryWrapper.like("create_by", noticeVo.getCreateBy());
        }
        if(noticeVo.getPageNum()!=null&&noticeVo.getPageSize()!=null){
            Page<Notice> page = new Page<>(noticeVo.getPageNum(), noticeVo.getPageSize());
            Page<Notice> p = noticeDao.selectPage(page, queryWrapper);
            return new R<>(200,"查询成功",p);
        }else{
            return new R<>(500,"查询失败",null);
        }


    }

    @Override
    public R<List<Notice>> selectAllNotice() {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0); // 添加条件，只查询 status 为 0 的记录
        List<Notice> list = noticeDao.selectList(queryWrapper);
        return new R<>(200, "查询成功", list);
    }

    @Override
    public R addNotice(NoticeVo noticeVo) {
        //获取token
        String token = request.getHeader("token");
        //根据token获取手机号
        String phone = (String) redisTemplate.opsForHash().get("user::login::" + token, "name");
        //根据手机号获取用户名
       QueryWrapper<User> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("phone", phone);
       User user = userDao.selectOne(queryWrapper);
       String username = user.getUserName();

        // 将用户名赋值给notice的createBy字段
        noticeVo.setCreateBy(username);
        int insert = noticeDao.insert1(noticeVo);
        if (insert > 0) {
            return new R<>(200, "添加成功", null);
        } else {
            return new R<>(500, "添加失败", null);
        }

    }

    @Override
    //根据id修改公告
    public R updateNotice(NoticeVo noticeVo) {
        //获取token
        String token = request.getHeader("token");
        //根据token获取手机号
        String phone = (String) redisTemplate.opsForHash().get("user::login::" + token, "name");
        //根据手机号获取用户名
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        User user = userDao.selectOne(queryWrapper);
        String username = user.getUserName();

        // 将用户名赋值给notice的updateBy字段
        noticeVo.setUpdateBy(username);
        int update = noticeDao.update1(noticeVo);
        if (update > 0) {
            return new R<>(200, "修改成功", null);
        } else {
            return new R<>(500, "修改失败", null);
        }
    }

    @Override
    //根据id查询公告--修改回显
    public R getNoticeById(Integer id) {
        Notice notice = noticeDao.selectById(id);
        if (notice != null) {
            return new R<>(200, "查询成功", notice);
        } else {
            return new R<>(500, "查询失败", null);
        }

    }

    @Override
    public R deleteNoticeByNoticeIds(List<Integer> noticeIds) {
        int delete = noticeDao.deleteNoticeByNoticeIds(noticeIds);
        if (delete > 0) {
            return new R<>(200, "删除成功", null);
        } else {
            return new R<>(500, "删除失败", null);
        }
    }

    @Override
    public R<List<News>> getLittleNews() {
        List<News> list = healtherNewsDao.selectList(null);
        if (list != null && list.size() > 0) {
            return new R<>(200, "查询成功", list);
        } else {
            return new R<>(500, "查询失败", null);
        }
    }

    @Override
    public R<News> getNewsByNewsId(String newsId) {
        News news = healtherNewsDao.selectById(newsId);
        if (news != null) {
            return new R<>(200, "查询成功", news);
        } else {
            return new R<>(500, "查询失败", null);
        }
    }
}
